/*
This file is part of Arcadeflex.

Arcadeflex is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Arcadeflex is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Arcadeflex.  If not, see <http://www.gnu.org/licenses/>.
 */
package flexfrontend;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.RowFilter;
import java.awt.Color;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableModel;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import java.awt.Toolkit;
import java.io.BufferedReader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JLabel;
import javax.swing.UIManager;


/**
 *
 * @author nickblame
 */
public class MainGUI extends javax.swing.JFrame {

    HashMap<String,String> romdescription=new HashMap<String,String>();
    private JLabel[] screens;
    //efaultListModel romlist = new DefaultListModel();
    myTableModel romlist = new myTableModel(false);
    TableRowSorter<TableModel> sorter;

    /** Creates new form MainGUI */
    public MainGUI() {
        initComponents();
        getListFull();
        Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("search.png")));
        jLabel6.setIcon(icon);
        filter.setBackground(Color.yellow);
        setTitle("flexFrontend " + Settings.version);
        setLocationRelativeTo(null);
        jCheckBox1.setSelected(Settings.scanlines);
        jCheckBox2.setSelected(Settings.log);
        cheatCheck.setSelected(Settings.cheat);
        soundBox.setSelected(Settings.soundEnabled);
        romTable.setModel(romlist);
        romTable.setAutoCreateRowSorter(true);
        
        //romTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        romTable.setShowGrid(false);
        romTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        filter.getDocument().addDocumentListener(new DocumentListener() {

            public void changedUpdate(DocumentEvent e) {
                updateFilter();
            }

            public void removeUpdate(DocumentEvent e) {
                updateFilter();
            }

            public void insertUpdate(DocumentEvent e) {
                updateFilter();
            }
        });
        sorter = new TableRowSorter<TableModel>(romlist);
        romTable.setRowSorter(sorter);
        //populate rom list:
        File folder = new File("roms/");
        if (folder.exists()) {
            File[] listOfFiles = folder.listFiles();
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isDirectory()) {
                    //System.out.println("File " + listOfFiles[i].getName());
                    if (!listOfFiles[i].getName().startsWith(".")) {
                        String description="";
                        description=romdescription.get(listOfFiles[i].getName());//check if there is a description available
                        String[] s = {listOfFiles[i].getName(),description};
                        romlist.addRow(s);
                    }
                } else if (listOfFiles[i].isDirectory()) {
                    //System.out.println("Directory " + listOfFiles[i].getName());
                }
            }
        }

        screens = new JLabel[3];
        screens[0] = jLabel3;
        screens[1] = jLabel4;
        screens[2] = jLabel5;
    }

    private void updateFilter() {
        String text = filter.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
            filter.setBackground(Color.yellow);
        } else {

            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text, 0));

            if (romTable.getRowCount() > 0) {
                filter.setBackground(Color.green);
            } else {
                filter.setBackground(Color.red);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        soundBox = new javax.swing.JCheckBox();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        previewPanel = new javax.swing.JPanel();
        flyerLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        descrArea = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cpuInfo = new javax.swing.JTextField();
        audioInfo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        screenshotsPane = new javax.swing.JTabbedPane();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        changelogTextArea = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        debugArea = new javax.swing.JTextArea();
        filter = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        romTable = new javax.swing.JTable();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        cheatCheck = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 51, 102));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jButton1.setText("load");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        soundBox.setText("sound");
        soundBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                soundBoxActionPerformed(evt);
            }
        });

        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });

        descrArea.setColumns(20);
        descrArea.setEditable(false);
        descrArea.setLineWrap(true);
        descrArea.setRows(5);
        descrArea.setWrapStyleWord(true);
        jScrollPane2.setViewportView(descrArea);

        javax.swing.GroupLayout previewPanelLayout = new javax.swing.GroupLayout(previewPanel);
        previewPanel.setLayout(previewPanelLayout);
        previewPanelLayout.setHorizontalGroup(
            previewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(previewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(flyerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addContainerGap())
        );
        previewPanelLayout.setVerticalGroup(
            previewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, previewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(previewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(flyerLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Info", previewPanel);

        jLabel1.setText("CPU");

        jLabel2.setText("Audio");

        cpuInfo.setEditable(false);

        audioInfo.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(audioInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cpuInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cpuInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(audioInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(273, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Details", jPanel1);

        screenshotsPane.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        screenshotsPane.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        screenshotsPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                screenshotsPaneStateChanged(evt);
            }
        });

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        screenshotsPane.addTab("1", jLabel3);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        screenshotsPane.addTab("2", jLabel4);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        screenshotsPane.addTab("3", jLabel5);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(screenshotsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(screenshotsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Screenshots", jPanel2);

        changelogTextArea.setColumns(20);
        changelogTextArea.setRows(5);
        jScrollPane4.setViewportView(changelogTextArea);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Changelog", jPanel4);

        debugArea.setColumns(20);
        debugArea.setRows(5);
        jScrollPane3.setViewportView(debugArea);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("DebugLog", jPanel3);

        romTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        romTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                romTableMouseReleased(evt);
            }
        });
        romTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                romTableKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(romTable);

        jCheckBox1.setText("scanlines");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jCheckBox2.setText("log");
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });

        cheatCheck.setText("cheat");
        cheatCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cheatCheckActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(filter, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cheatCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBox2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(soundBox)
                        .addGap(21, 21, 21))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(soundBox)
                            .addComponent(jCheckBox1)
                            .addComponent(jCheckBox2)
                            .addComponent(jButton1)
                            .addComponent(cheatCheck)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (romTable.getSelectedRow() != -1) {

            String[] a = {(String) sorter.getModel().getValueAt(romTable.convertRowIndexToModel(romTable.getSelectedRow()), 0)};
            try {
                String Options="";
                if(jCheckBox2.isSelected())
                {
                    Options+= " -log";
                }
                if(!jCheckBox1.isSelected())
                {
                    Options+=" -noscanlines";
                }
                if(!soundBox.isSelected())
                {
                    Options+=" -nosound";
                }
                if(cheatCheck.isSelected())
                {
                    Options+=" -cheat";
                }
                runarcadeflex(a[0],Options);
            } catch (IOException ex) {
                Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void soundBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_soundBoxActionPerformed
        Settings.soundEnabled = soundBox.isSelected();
    }//GEN-LAST:event_soundBoxActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed

    }//GEN-LAST:event_formKeyPressed

    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged
        // TODO add your handling code here:
        updateEverything();
    }//GEN-LAST:event_jTabbedPane1StateChanged

    private void screenshotsPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_screenshotsPaneStateChanged
        // TODO add your handling code here:
        updateEverything();
}//GEN-LAST:event_screenshotsPaneStateChanged

    private void romTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_romTableKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_romTableKeyPressed

    private void romTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_romTableMouseReleased
        // TODO add your handling code here:
        updateEverything();
    }//GEN-LAST:event_romTableMouseReleased

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        Settings.scanlines = jCheckBox1.isSelected();
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
        // TODO add your handling code here:
        Settings.log = jCheckBox2.isSelected();
    }//GEN-LAST:event_jCheckBox2ActionPerformed

private void cheatCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cheatCheckActionPerformed
       Settings.cheat=cheatCheck.isSelected();
}//GEN-LAST:event_cheatCheckActionPerformed
    public void updateEverything() {
        int lastselection = -1;
        int newselection = romTable.getSelectedRow();
        
        if (newselection != -1 && newselection!=lastselection) {
            lastselection = newselection;
            if (screenshotsPane.isShowing()) {
                new LoadImageThread("flexfrontend/screenshots/" + (String) sorter.getModel().getValueAt(romTable.convertRowIndexToModel(romTable.getSelectedRow()), 0) + "/" + (screenshotsPane.getSelectedIndex() + 1) + ".png", screens[screenshotsPane.getSelectedIndex()]).start();
            }
            if (flyerLabel.isShowing()) {
                new LoadImageThread("flexfrontend/flyers/" + (String) sorter.getModel().getValueAt(romTable.convertRowIndexToModel(romTable.getSelectedRow()), 0) + ".png", flyerLabel).start();
                new LoadTextThread("flexfrontend/info/" + (String) sorter.getModel().getValueAt(romTable.convertRowIndexToModel(romTable.getSelectedRow()), 0) + ".ini", descrArea, cpuInfo, audioInfo).start();
            }
            if (cpuInfo.isShowing()) {
                new LoadTextThread("flexfrontend/info/" + (String) sorter.getModel().getValueAt(romTable.convertRowIndexToModel(romTable.getSelectedRow()), 0) + ".ini", descrArea, cpuInfo, audioInfo).start();

            }
            if(changelogTextArea.isShowing())
            {
                new LoadChangelogThread("flexfrontend/changelog/" + (String) sorter.getModel().getValueAt(romTable.convertRowIndexToModel(romTable.getSelectedRow()), 0), changelogTextArea).start();
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e){
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainGUI().setVisible(true);
            }
        });
    }
     String outlist[];
    public void runarcadeflex(String game,String options) throws IOException {
      debugArea.setText("");
      String cmd = "java -jar arcadeflex.jar " + game + "" + options;
     // System.out.println(cmd);
      new RunArcadeflexThread(game,cmd,debugArea).start();
      /*
      try {
        // Run a game
        outlist = runCommand(cmd);
        // Display its output
        for (int i = 0; i < outlist.length; i++) {
            System.out.println(outlist[i]); 
            debugArea.append(outlist[i] + " \n");
	}
      
      }
      catch (IOException e) {
        System.err.println(e);
      }*/
    }
  

  static public String[] runCommand(String cmd) throws IOException {
    // Set up list to capture command output lines
    ArrayList list = new ArrayList();

    // Start command running
    Process proc = Runtime.getRuntime().exec(cmd);

    // Get command's output stream and
    // put a buffered reader input stream on it
    InputStream istr = proc.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(istr));

    // Read output lines from command
    String str;
    while ((str = br.readLine()) != null) list.add(str);

    // Wait for command to terminate
    try {
      proc.waitFor();
    }
    catch (InterruptedException e) {
      System.err.println("Process was interrupted");
    }

    // Check its exit value
    if (proc.exitValue() != 0)
      System.err.println("Exit value was non-zero");

    // Close stream
    br.close();

    // Return list of strings to caller
    return (String[])list.toArray(new String[0]);
  }
  /*
   * This method retrieves list of supported games with full description
   */
  public void getListFull() {
    ArrayList listFull = new ArrayList();

    try {
      // Run command 'xmame -listfull'
      outlist = runCommand("java -jar arcadeflex.jar " + " -listfull");

      for (int i = 1; i < outlist.length; i++) { //skip line 1 it contains titles
        String line = outlist[i];
        int findspace=line.indexOf(" ");
        String romname=line.substring(0, findspace); 
        int findeisag=line.indexOf('\"'); 
        int findlastisag=line.indexOf('\"',findeisag+1); 
        String description=line.substring(findeisag+1, findlastisag);
        romdescription.put(romname, description);
      }
    }
    catch (IOException e) {
      System.err.println(e);
    }
    
   }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField audioInfo;
    private javax.swing.JTextArea changelogTextArea;
    private javax.swing.JCheckBox cheatCheck;
    private javax.swing.JTextField cpuInfo;
    private javax.swing.JTextArea debugArea;
    private javax.swing.JTextArea descrArea;
    private javax.swing.JTextField filter;
    private javax.swing.JLabel flyerLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel previewPanel;
    private javax.swing.JTable romTable;
    private javax.swing.JTabbedPane screenshotsPane;
    private javax.swing.JCheckBox soundBox;
    // End of variables declaration//GEN-END:variables
}
